package com.api.main;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;
import org.apache.http.impl.client.DefaultHttpClient; 
import java.util.ArrayList;  
import java.util.Iterator;  
import java.util.List;  
import java.util.Map;  
import java.util.Map.Entry;  
import org.apache.http.HttpEntity;  
import org.apache.http.HttpResponse;  
import org.apache.http.NameValuePair;  
import org.apache.http.client.HttpClient;  
import org.apache.http.client.entity.UrlEncodedFormEntity;  
import org.apache.http.client.methods.HttpPost;  
import org.apache.http.message.BasicNameValuePair;  
import org.apache.http.util.EntityUtils;

import com.api.util.HttpUtil;

import java.security.cert.CertificateException;  
import java.security.cert.X509Certificate;  
import javax.net.ssl.SSLContext;  
import javax.net.ssl.TrustManager;  
import javax.net.ssl.X509TrustManager;  
import org.apache.http.conn.ClientConnectionManager;  
import org.apache.http.conn.scheme.Scheme;  
import org.apache.http.conn.scheme.SchemeRegistry;  
import org.apache.http.conn.ssl.SSLSocketFactory; 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class test {

	public static void main(String[] args) throws KeyManagementException, NoSuchAlgorithmException {
		String postUr;
		String userid;
		String appkey;
		String deviceid;
		String timestamp;
		String token;
		String refUrl;
		
		String Cookie;
		
		
		try {
			//发短信
//			postUr="https://api.accident.zhongchebaolian.com/industryguild_mobile_standard_self2.1.2/mobile/standard/checkImageVerification";
//			//System.out.println(lpam.toString());
//			//System.out.println(HttpUtil.getURLEncoderString(lpam.toString()));
//			System.out.println("fa"+HttpUtil.Post(postUr,"%7B%22phone%22%3A%2215666777688%22%2C%22smsflag%22%3A%2201%22%2C%22imagecode%22%3A%22g3%E8%B4%B1g%22%2C%22method%22%3A%22checkImageVerification%22%2C%22timestamp%22%3A%222017-04-27+15%3A05%3A42%22%2C%22deviceid%22%3A%22865479022050306%22%2C%22sign%22%3A%2201D2819C36F605C46437E43C3861E2D4%22%7D","utf-8", false,""));
//			
//
//			//第一步短信验证码
//		    //https://api.accident.zhongchebaolian.com/industryguild_mobile_standard_self2.1.2/mobile/standard/login
//			postUr="https://api.accident.zhongchebaolian.com/industryguild_mobile_standard_self2.1.2/mobile/standard/login";
//			JSONObject lpam=new JSONObject();
//			lpam.put("phone", "15666777688");
//			lpam.put("valicode", "399189");
//			lpam.put("devicetype", "1");
//			lpam.put("vertype", "1");
//			lpam.put("deviceid", UUID.randomUUID().toString());
//			lpam.put("source", "0");
//			lpam.put("token", "1101");
			//System.out.println(lpam.toString());
			//System.out.println(HttpUtil.getURLEncoderString(lpam.toString()));
			//HttpUtil.Post(postUr,HttpUtil.getURLEncoderString(lpam.toString()),"utf-8", false,"");
			
			//第一步短信验证码
			postUr="https://api.accidentx.zhongchebaolian.com/mobile/mobile/global/verification";
			//HttpUtil.Post(postUr,"%7B%22phone%22%3A%2215666777688%22%7D","utf-8", false,"");
			
	        //第二步登陆 	
			postUr="https://api.accidentx.zhongchebaolian.com/mobile/mobile/global/login";
			JSONObject pam=new JSONObject();
			pam.put("phone", "15666777688");
			pam.put("valicode", "399189");
			pam.put("devicetype", "1");
			pam.put("vertype", "1");
			pam.put("deviceid", UUID.randomUUID().toString());
			pam.put("source", "0");
			pam.put("citycode", "1101");
			System.out.println(pam.toString());
			System.out.println(HttpUtil.getURLEncoderString(pam.toString()));
			//HttpUtil.Post(postUr,HttpUtil.getURLEncoderString(pam.toString()),"utf-8", false,"");
			//{"citycode":"","userid":"D8F1301A24C04ACFA904ED8235F9910D","provincecode":null,"userType":"1","accesstoken":null,"policeno":"","ssid":"BQSXZ460595856247904524437","provincetiny":null,"rescode":"200","resdes":"登陆成功","ssidcode":null}
			
			//第三步获取车辆信息
			//userid=D8F1301A24C04ACFA904ED8235F9910D&appkey=kkk&deviceid=ddd&timestamp=1493258176000&token=D04F591C87621B9DEFFCE852511A0A39&appsource=jjzx
			postUr="https://api.jinjingzheng.zhongchebaolian.com/enterbj/platform/enterbj/entercarlist";
			String pamS="";
			pamS="userid="+"D8F1301A24C04ACFA904ED8235F9910D";
			//pamS=pamS+"&"+"tooken="+UUID.randomUUID().toString();
			pamS=pamS+"&"+"appsource="+"jjzx";
			System.out.println(pamS);
			//System.out.println(HttpUtil.Post(postUr,pamS,"UTF-8", false,""));
			
			
			//第四步获取要提交信息
			postUr="https://api.jinjingzheng.zhongchebaolian.com/enterbj/platform/enterbj/applyBjMessage";
			pamS="";
			pamS="userid="+"d48c19282d5c4cddbb0d2771cc4815c9";
			//pamS=pamS+"&"+"tooken="+"D04F591C87621B9DEFFCE852511A0A39";
			pamS=pamS+"&"+"applyid="+"067201704260920329501657";
			pamS=pamS+"&"+"carid="+"17651";
			//pamS=pamS+"&"+"imei="+"22622b98-a440-31e2-af8d-978dfefd2f3c";
			pamS=pamS+"&"+"licenseno="+"%E5%86%80J198FJ";
			//pamS=pamS+"&"+"envGrade="+"3";
			pamS=pamS+"&"+"carmodel="+"SY7150WSBAB";
			pamS=pamS+"&"+"carregtime="+"2012-01-12";
			pamS=pamS+"&"+"appsource=bjjj"+"";
			System.out.println(pamS);
			System.out.println(HttpUtil.Post(postUr,pamS,"UTF-8", false,"").getBytes().length);
			
			
			//System.out.println(new String(HttpUtil.post(postUr,"phone=15666777688&smsflag=01&imagecode=111111&method=checkImageVerification","utf-8")));
//	        Map<String,String> createMap = new HashMap<String,String>();  
//	        createMap.put("phone","1566677688");  
//					System.out.println(new test().doPost(postUr,createMap,"utf-8"));
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public class SSLClient extends DefaultHttpClient{  
	    public SSLClient() throws Exception{  
	        super();  
	        SSLContext ctx = SSLContext.getInstance("TLS");  
	        X509TrustManager tm = new X509TrustManager() {  
	                @Override  
	                public void checkClientTrusted(X509Certificate[] chain,  
	                        String authType) throws CertificateException {  
	                }  
	                @Override  
	                public void checkServerTrusted(X509Certificate[] chain,  
	                        String authType) throws CertificateException {  
	                }  
	                @Override  
	                public X509Certificate[] getAcceptedIssuers() {  
	                    return null;  
	                }  
	        };  
	        ctx.init(null, new TrustManager[]{tm}, null);  
	        SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  
	        ClientConnectionManager ccm = this.getConnectionManager();  
	        SchemeRegistry sr = ccm.getSchemeRegistry();  
	        sr.register(new Scheme("https", 443, ssf));  
	    }  
	}
	 public String doPost(String url,Map<String,String> map,String charset){  
	        HttpClient httpClient = null;  
	        HttpPost httpPost = null;  
	        String result = null;  
	        try{  
	            httpClient = new SSLClient();  
	            httpPost = new HttpPost(url);  
	            //设置参数  
	            List<NameValuePair> list = new ArrayList<NameValuePair>();  
	            Iterator iterator = map.entrySet().iterator();  
	            while(iterator.hasNext()){  
	                Entry<String,String> elem = (Entry<String, String>) iterator.next();  
	                list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));  
	            }  
	            if(list.size() > 0){  
	                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);  
	                httpPost.setEntity(entity);  
	            }  
	            HttpResponse response = httpClient.execute(httpPost);  
	            if(response != null){  
	                HttpEntity resEntity = response.getEntity();  
	                if(resEntity != null){  
	                    result = EntityUtils.toString(resEntity,charset);  
	                }  
	            }  
	        }catch(Exception ex){  
	            ex.printStackTrace();  
	        }  
	        return result;  
	    }  
	 
	 
	 
}
